//
//  MiddleSwift.swift
//  ProjectDemo
//
//  Created by caoxiaozhu on 2018/8/10.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

import UIKit

class MiddleSwift: UIViewController {

    var a : Int  = 1
    
    //计算型属性，在大括号里面计算完再调用
    fileprivate var name : String {
        let realName : String = "\(a)个老王"
        return realName
    }
    
    //懒加载
    fileprivate lazy var lazyName : String = {
        let lazyName : String = "\(self.a)个老王"
        return lazyName
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
//        test1()
//        let person : String =  test2(name: "老王", age: 32)
//        let person : String =  test2("老王", 32)
//        let person : String = test2(name: "老王", age: 32)
//        print(person)
//        test4()
//        test5 { (jsonArr) in
//            print(jsonArr)
//        }
//        test6()
//        test7()
        test8()
    }

    //TODO: 计算型属性和懒加载
    func test1(){
        //        a = 3
        //懒加载是不会储存值的，只有计算型属性能够储存或者计算
        print("name == > \(name)")
        print("lazyName ==> \(lazyName)")
    }
    
    //点击改变数量看计算型属性和懒加载的状态
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        print("---------------------------------")
        a = 3
        //懒加载是不会储存值的，只有计算型属性能够储存或者计算
        print("name == > \(name)")
        print("lazyName ==> \(lazyName)")
        
    }
    
    //TODO: 函数
//    func test2(name:String,age:Int) -> String {
//        let person : String  = "name => \(name), age ==>\(age)"
//        return person
//    }
    //外部参数
//    func test2(_ name:String, _ age:Int) -> String {
//        let person : String  = "name => \(name), age ==>\(age)"
//        return person
//    }
    
    func test2(name name:String, age age:Int) -> String {
        let person : String  = "name => \(name), age ==>\(age)"
        return person
    }
    
    //TODO: 闭包
    func test3(){
        let sum = {(num1:Int, num2:Int) -> Int in
            return num1 + num2
        }
        
        print("sum==>\(sum(1,2))")
    }
    
    
    //TODO: GCD
    func test4(){
        DispatchQueue.global().async {
            print("耗时操作-----\(Thread.current)")
        }
//        DispatchQueue.global()
    }
    
    //TODO: 根据闭包返回参数
    func test5(successBlock: @escaping ([String]) -> ()){
        DispatchQueue.global().async {
            Thread.sleep(forTimeInterval: 1)
            let json = ["好的","不好的","一般般"]
            DispatchQueue.main.async(execute: {
                successBlock(json)
            })
        }
    }
    
    //TODO: 对象
    func test6(){
        let p = PersonTest()
        p.name = "张三"
        print(p.name)
    }
    
    //TODO: 重载
    func test7(){
        let p  = PersonTest(name: "李四")
        print(p.name);
    }
    
    //TODO: KVC基本实现
    func test8(){
        let s = Student(dict: ["name":"战地三","age":"18","title":"boss"])
        print("\(s.age ?? "") \(s.title ?? "" )")
    }
}
